package com.gexiaobao.pigeon.viewmodel

import android.view.View
import androidx.databinding.ObservableInt
import androidx.lifecycle.MutableLiveData
import com.gexiaobao.pigeon.app.model.bean.GatherListResponse
import com.gexiaobao.pigeon.app.model.bean.MemberCodeResponse
import com.gexiaobao.pigeon.app.model.bean.PickUpDetailResponse
import com.gexiaobao.pigeon.app.model.bean.PigeonBean
import com.gexiaobao.pigeon.app.model.bean.RaceDetailInfoResponse
import com.gexiaobao.pigeon.app.model.bean.UiState
import com.gexiaobao.pigeon.app.model.bean.VisitMemberListResponse
import com.gexiaobao.pigeon.app.model.param.SignUpParams
import com.gexiaobao.pigeon.app.network.ListDataUiState
import com.gexiaobao.pigeon.app.network.apiService
import com.gexiaobao.pigeon.app.util.RxToast
import me.hgj.jetpackmvvm.base.viewmodel.BaseViewModel
import me.hgj.jetpackmvvm.callback.databind.BooleanObservableField
import me.hgj.jetpackmvvm.callback.databind.StringObservableField
import me.hgj.jetpackmvvm.ext.request

/**
 * @Author      : hxw
 * @Date        :2023/2/8 8:59
 * @Describe    :
 */
class TrainingInfoViewModel : BaseViewModel() {

    private var pageSize = 50
    private var pageNo = 1

    var signUp = StringObservableField()
    var signUpContent = StringObservableField()
    var isShowSignUp = BooleanObservableField()

    //训放信息
    var trainingSignUpPigeonTime = StringObservableField()
    var trainingTime = StringObservableField()
    var trainingPlace = StringObservableField()
    var flyDissance = StringObservableField()
    var trainingLon = StringObservableField()
    var trainingLat = StringObservableField()
    var trainingWeather = StringObservableField()
    var trainingTemperature = StringObservableField()
    var trainingWindPower = StringObservableField()
    var trainingSignUpNo = StringObservableField()
    var trainingIntoNo = StringObservableField()
    var trainingBackNo = StringObservableField()

    /**  训放详细信息  */
    var detailInfoResult: MutableLiveData<RaceDetailInfoResponse> = MutableLiveData()

    var pigeonData: MutableLiveData<ListDataUiState<PigeonBean.RingInfo>> = MutableLiveData()

    /** 报名训放 */
    val signUpTrainingResult: MutableLiveData<UiState> = MutableLiveData()

    /** 取消报名训放 */
    val cancelTrainingResult: MutableLiveData<UiState> = MutableLiveData()

    /** 训放赛报名详情列表信息 */
    val trainingSignUpDetailListResult: MutableLiveData<ListDataUiState<GatherListResponse.DataList>> = MutableLiveData()

    /** 获取接鸽详情 */
    val pickUpPigeonResult: MutableLiveData<ListDataUiState<PickUpDetailResponse.PostAdInfo>> = MutableLiveData()

    /**  获取棚号  */
    var memberNumResult: MutableLiveData<MemberCodeResponse> = MutableLiveData()

    /** 是否显示报名按钮 */
    var showSignUp = object : ObservableInt(isShowSignUp) {
        override fun get(): Int {
            return if (isShowSignUp.get()) {
                View.VISIBLE
            } else {
                View.GONE
            }
        }
    }

    /** 获取单用户下探视棚号列表  */
    val visitMemberResult: MutableLiveData<VisitMemberListResponse> = MutableLiveData()
    fun getVisitMemberNoList(raceId: String) {
        request({ apiService.getVisitMemberNoList(raceId) }, {
            visitMemberResult.value = it
        }, {
            RxToast.warning(it.msg)
        })
    }

    fun getPassDetailApp(id: String) {
        request({ apiService.getPassDetailApp(id) }, {
            detailInfoResult.value = it
        }, {
            RxToast.showToast(it.msg)
        })
    }

    /** 获取我的赛鸽列表列表 */
    fun getPigeonList(isRefresh: Boolean, type: String) {
        val body = hashMapOf<String, Any>().apply {
            put("type", type)
        }
        request({ apiService.getPigeonList(body) }, {
            val listDataUiState = ListDataUiState(
                isSuccess = true,
                isRefresh = isRefresh,
                isEmpty = it.isEmpty(),
                total = it.total,
                hasMore = false,
                isFirstEmpty = isRefresh && it.isEmpty(),
                listData = it.ringInfo
            )
            pigeonData.value = listDataUiState
        }, {
            val listDataUiState = ListDataUiState(
                isSuccess = false, errMessage = it.msg, isRefresh = isRefresh, listData = arrayListOf<PigeonBean.RingInfo>()
            )
            pigeonData.value = listDataUiState
        })
    }

    //训放赛报名
    fun getSignUpTraining(params: SignUpParams) {
        request({ apiService.getSignUpTraining(params) }, {
            val deleteState = UiState(isSuccess = true)
            signUpTrainingResult.value = deleteState
        }, {
            val deleteState = UiState(isSuccess = false, errorMsg = it.msg)
            signUpTrainingResult.value = deleteState
        })
    }

    //训放赛取消报名
    fun cancelTraining(params: SignUpParams) {
        request({ apiService.cancelTraining(params) }, {
            val deleteState = UiState(isSuccess = true)
            cancelTrainingResult.value = deleteState
        }, {
            val deleteState = UiState(isSuccess = false, errorMsg = it.msg)
            cancelTrainingResult.value = deleteState
        })
    }

//    fun getGatherList(isRefresh: Boolean, passId: String, qStr: String) {
//        if (isRefresh) {
//            pageNo = 1
//        }
//        request({ apiService.getGatherList(passId, qStr, "", 50, pageNo) }, {
//            pageNo++
//            val listDataUiState = ListDataUiState(
//                isSuccess = true,
//                isRefresh = isRefresh,
//                isEmpty = it.isEmpty(),
//                total = it.total,
//                hasMore = it.hasMore(),
//                isFirstEmpty = isRefresh && it.isEmpty(),
//                listData = it.data
//            )
//            trainingSignUpDetailListResult.value = listDataUiState
//        }, {
//            val listDataUiState =
//                ListDataUiState(
//                    isSuccess = false,
//                    isRefresh = isRefresh,
//                    errCode = it.code,
//                    errMessage = it.msg,
//                    listData = arrayListOf<GatherListResponse.DataList>()
//                )
//            trainingSignUpDetailListResult.value = listDataUiState
//        })
//    }

    /** 训放赛报名详情列表信息 */
    val gatherDetailResponse: MutableLiveData<GatherListResponse> = MutableLiveData()
    fun getGatherListForHand(passId: String, qStr: String, pageNo: Int) {
        request({ apiService.getGatherListForHand(passId, qStr, "", 50, pageNo) }, {
            gatherDetailResponse.value = it
        }, {
            RxToast.showToast(it.msg)
        })
    }

    /** 接鸽详情 */
    fun getGatherPost(raceId: String) {
        request({ apiService.getGatherPost(raceId) }, {
            val listDataUiState = ListDataUiState(
                isSuccess = true, isRefresh = true, isEmpty = it.isEmptyAd(), isFirstEmpty = it.isEmptyAd(), listData = it.postAd
            )
            pickUpPigeonResult.value = listDataUiState
        }, {
            val listDataUiState =
                ListDataUiState(isSuccess = false, isRefresh = true, errMessage = it.msg, listData = arrayListOf<PickUpDetailResponse.PostAdInfo>())
            pickUpPigeonResult.value = listDataUiState
        })
    }

    //获取棚号
    fun getMemberNum(id: String) {
        request({ apiService.getMemberNum(id) }, {
            memberNumResult.value = it
        }, {
            RxToast.showToast(it.msg)
        })
    }
}